using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using Decav.Data;
using Decav.Gatsb.Application;

namespace Decav.Gatsb.Data
{
    /// <summary>
    /// Lazy loads a collection of <see cref="PostAttachment"/> for a particular post.
    /// </summary>
    [Serializable]
    public class PostAttachmentCollectionLazyLoadReference : LazyLoadReference<Guid, Collection<PostAttachment>>
    {
        public PostAttachmentCollectionLazyLoadReference()
            : base(Guid.Empty, new Collection<PostAttachment>())
        {
        }

        /// <summary>
        /// Initializes a new <see cref="PostAttachmentCollectionLazyLoadReference"/>.
        /// </summary>
        /// <param name="postId">The unique identifier of the post to load the attachments for.</param>
        public PostAttachmentCollectionLazyLoadReference(Guid postId)
            : base(postId)
        {
        }

        protected override Collection<PostAttachment> LoadValue()
        {
            using (IGatsbDataAdapter adapt = GatsbApplication.Current.GetDataAdapter())
                return new Collection<PostAttachment>(adapt.GetPostAttachments(Id));
        }
    }
}
